/*
**  Android Remote for Tux (AndroTux) 
**  Copyright (C) 2014 Itay Rom <1t4yr0m@gmail.com> Tal Sabo <tal.sabo@gmail.com> 
**
**  This program is free software: you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation, either version 3 of the License, or
**  (at your option) any later version.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.androtux;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TabBarFragment extends Fragment {
	private CommunicationHandler _cHandler;
	private AlertDialog.Builder _builder;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	        Bundle savedInstanceState) {
	    View rootView = inflater.inflate(R.layout.fragment_tabbar, container, false);
	    
	    _cHandler = CommunicationHandler.getInstance();
	    
	    _builder = new AlertDialog.Builder(getActivity());
	    _builder.setMessage(R.string.must_connect_first_msg).setTitle(R.string.error_alertdialog_title).setPositiveButton("Ok", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
				
			}
		});
	    
	    ToggleButton tbKeyboard = (ToggleButton)rootView.findViewById(R.id.tbKeyboard);
	    ToggleButton tbMouse = (ToggleButton)rootView.findViewById(R.id.tbMouse);
	    ToggleButton tbGamepad = (ToggleButton)rootView.findViewById(R.id.tbGamepad);
	    final ToggleButton tbSettings = (ToggleButton)rootView.findViewById(R.id.tbSettings);
	    ToggleButton tbAbout = (ToggleButton)rootView.findViewById(R.id.tbAbout);

	    
	    tbKeyboard.addOnOps(new Runnable() {
			@Override
			public void run() {
					if (_cHandler.isConnected()) {
						FragmentTransaction transaction = getFragmentManager().beginTransaction();
						transaction.replace(R.id.item_fragment, new KeyboardFragment());
						transaction.addToBackStack(null);
						transaction.commit();
					} else {
						_builder.show();
						tbSettings.setOn();
					}
			}
		});
	    
	    tbMouse.addOnOps(new Runnable() {
			@Override
			public void run() {
				if (_cHandler.isConnected()) {
					FragmentTransaction transaction = getFragmentManager().beginTransaction();
					transaction.replace(R.id.item_fragment, new MouseFragment());
					transaction.addToBackStack(null);
					transaction.commit();
				} else {
					_builder.show();
					tbSettings.setOn();
				}
			}
		});
	    
	    tbGamepad.addOnOps(new Runnable() {
			@Override
			public void run() {
				if (_cHandler.isConnected()) {
					FragmentTransaction transaction = getFragmentManager().beginTransaction();
					transaction.replace(R.id.item_fragment, new GamepadFragment());
					transaction.addToBackStack(null);
					transaction.commit();
				} else {
					_builder.show();
					tbSettings.setOn();
				}
			}
		});
	    
	    tbSettings.addOnOps(new Runnable() {
			@Override
			public void run() {
				FragmentTransaction transaction = getFragmentManager().beginTransaction();
				transaction.replace(R.id.item_fragment, new SettingsFragment());
				transaction.addToBackStack(null);
				transaction.commit();
			}
		});
	    
	    tbAbout.addOnOps(new Runnable() {
			@Override
			public void run() {
				FragmentTransaction transaction = getFragmentManager().beginTransaction();
				transaction.replace(R.id.item_fragment, new AboutFragment());
				transaction.addToBackStack(null);
				transaction.commit();
			}
		});
	    
	    tbKeyboard.setImage(R.drawable.ic_action_keyboard);
	    tbMouse.setImage(R.drawable.ic_action_mouse);
	    tbGamepad.setImage(R.drawable.ic_action_gamepad);
	    tbSettings.setImage(R.drawable.ic_action_settings);
	    tbAbout.setImage(R.drawable.ic_action_about);
	    
	    tbKeyboard.addListener(tbMouse);
	    tbKeyboard.addListener(tbGamepad);
	    tbKeyboard.addListener(tbSettings);
	    tbKeyboard.addListener(tbAbout);
	    
	    tbMouse.addListener(tbKeyboard);
	    tbMouse.addListener(tbGamepad);
	    tbMouse.addListener(tbSettings);
	    tbMouse.addListener(tbAbout);
	    
	    tbGamepad.addListener(tbKeyboard);
	    tbGamepad.addListener(tbMouse);
	    tbGamepad.addListener(tbSettings);
	    tbGamepad.addListener(tbAbout);
	    
	    tbSettings.addListener(tbKeyboard);
	    tbSettings.addListener(tbMouse);
	    tbSettings.addListener(tbGamepad);
	    tbSettings.addListener(tbAbout);
	    
	    tbAbout.addListener(tbKeyboard);
	    tbAbout.addListener(tbMouse);
	    tbAbout.addListener(tbGamepad);
	    tbAbout.addListener(tbSettings);

	    tbSettings.setOn();
	    
	    return rootView;
	}
}
